package backend

import (
	"fmt"
	"gitee.com/jjawesomejj/awesome-util/config"
	"gitee.com/jjawesomejj/awesome-util/event"
	WsServer "gitee.com/jjawesomejj/awesomehttp/system/wsServer"
	"github.com/gorilla/websocket"
	"strconv"
)

var GwsServer WsServer.WsServer

func RunWsServer() {
	GwsServer = WsServer.WsServer{}
	portString := config.GetConfigByKey("httpPort", func() interface{} {
		return "80"
	}).(string)
	portInt, err := strconv.Atoi(portString)
	if err != nil {
		panic("httpPort need string params")
	}
	GwsServer.OnStart(func() {
		fmt.Println("websocket 服务启动成功")
	})

	GwsServer.OnMessage(func(connection *websocket.Conn, message string) {

	})

	GwsServer.OnConnect(func(connection *websocket.Conn) {

	})
	event.Register("onlog", func(args ...interface{}) {
		for _, con := range GwsServer.GetConnections() {
			err := con.WriteJSON(args[0])
			if err != nil {
				fmt.Println("ws_err", err)
				continue
			}
		}
	})
	GwsServer.Start(portInt, "ws", false, true)
}
